package com.zzu.base.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.*;
import jakarta.annotation.Resource;
import org.apache.ibatis.jdbc.Null;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.time.LocalDateTime;

@Configuration
public class MybatisPlusConfig {

    @Resource
    private Environment environment;

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        PaginationInnerInterceptor pagination = new PaginationInnerInterceptor(DbType.MYSQL);
        pagination.setOverflow(true); // 页码溢出时返回第一页
        pagination.setMaxLimit(500L); // 单页最大条数限制
        interceptor.addInnerInterceptor(pagination);

        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());


        return interceptor;
    }

    // 自动填充公共字段
    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new MetaObjectHandler() {@Override
        public void insertFill(MetaObject metaObject) {
            strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
            strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
            strictInsertFill(metaObject, "creator", String.class, getCurrentUser());
            strictInsertFill(metaObject, "updater", String.class, getCurrentUser());
        }

            @Override
            public void updateFill(MetaObject metaObject) {
                strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
                strictUpdateFill(metaObject, "updater", String.class, getCurrentUser());
            }

            private String getCurrentUser() {
                Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

                // 检查是否有认证信息
                if (authentication == null || !authentication.isAuthenticated()) {
                    return "SYSTEM";
                }

                // 处理匿名用户
                if (authentication instanceof AnonymousAuthenticationToken) {
                    return "ANONYMOUS";
                }
                return authentication.getName();
            }
        };
    }
}